<?php 
	class Manage_gift extends CI_Controller{
		public  function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_gift");
			$this->load->model("apanel/db_news");
			$this->load->library('pagination');
		}
		
		public function index(){
			redirect("manage_gift/gift_list");
		}
		
		public function gift_list($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			list($item) = $this->db_gift->gift_list();
			$this->cismarty->assign("item",$item);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
				
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,3));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open('manage_gift/gift_detAll'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/gift/gift_list.html');
		}
		
		public function gift_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("title_th","name","required");
				$this->form_validation->set_rules("desc_th","Description","trim");
				$this->form_validation->set_rules("customer","Customer","required");
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
			
					//set error
					$this->cismarty->assign("errortitle_th",form_error("title_th"));
					$this->cismarty->assign("errorstatus",form_error("status"));
					$this->cismarty->assign("errorcustomer",form_error('customer'));
				}else {
					$error = false;
					$picname = "";
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/news/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 256;
							$config['height'] = 220;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"name"=>trim($this->input->post('title_th')),
								"content"=>addslashes(trim($this->input->post('desc_th'))),
								"customer_pkid"=>$this->input->post("customer"),
								"img"=>$picname,
								"status"=>$this->input->post('status'),
								"created_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s"),
								"gift_type"=>$this->input->post("gift_type"),
								"gift_number"=>$this->input->post("gift_num")
						);
						$res = $this->db->insert('mobile_gift',$data);
						if($res) redirect('manage_gift/gift_list/success');
						else  redirect('manage_gift/gift_list/fail');
					}
				}
			}//end post		
			$this->cismarty->assign("tiny",$this->template->tinymce());
			$this->cismarty->assign("customer",$this->db_news->customer_list());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,3));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart('manage_gift/gift_add'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/gift/gift_add.html');
		}
		
		public function gift_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
				
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("title_th","name","required");
				$this->form_validation->set_rules("desc_th","Description","trim");
					
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
						
					//set error
					$this->cismarty->assign("errortitle_th",form_error("title_th"));
					$this->cismarty->assign("errorstatus",form_error("status"));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/news/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 256;
							$config['height'] = 220;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							$uppic = array("img"=>$picname);
							$this->db->where("pkid",$id);
							$this->db->update('mobile_gift',$uppic);
						}else{
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"name"=>trim($this->input->post('title_th')),
								"content"=>addslashes(trim($this->input->post('desc_th'))),
								"status"=>$this->input->post('status'),
								"created_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s"),
								"gift_type"=>$this->input->post("gift_type"),
								"gift_number"=>$this->input->post("gift_num")
						);
						$this->db->where("pkid",$id);
						$res = $this->db->update('mobile_gift',$data);
						if($res) redirect('manage_gift/gift_list/success');
						else  redirect('manage_gift/gift_list/fail');
					}
				}
			}//end post
			else{
				$this->db->where("pkid",$id);
				$rows = $this->db->get("mobile_gift")->row();
				$this->cismarty->assign("title_th",$rows->name);
				$this->cismarty->assign("desc_th",stripslashes($rows->content));
				$this->cismarty->assign("gift_type",$rows->gift_type);
				$str_type = "";
				if($rows->gift_type == 1){
					$str_type .='<option value="1"  selected>Other</option>';
					$str_type .='<option value="2">Limit</option>';
				}else{
					$str_type .='<option value="1" >Other</option>';
					$str_type .='<option value="2"  selected>Limit</option>';
				}
				$this->cismarty->assign("str_type",$str_type);
				$this->cismarty->assign("str_num",$rows->gift_number);
				if(trim($rows->img) != ""){
					$this->cismarty->assign("img",'<img src="'.base_url().'media/news/'.$rows->img.'"><br>');
				}
				if($rows->status == 1) $this->cismarty->assign("sa",'checked="checked"');
				else $this->cismarty->assign("sd",'checked="checked"');
				$customer = $rows->customer_pkid;
			}
			$this->cismarty->assign("customer",$this->db_news->customer_list($customer));
			$this->cismarty->assign("tiny",$this->template->tinymce());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,3));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_gift/gift_edit/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/gift/gift_edit.html');
		}
		
		public function gift_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_gift");
				if($res) redirect('manage_gift/gift_list/success');
			}
		}
		
		public function gift_detAll(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("mobile_gift");
				}
			}
			redirect('manage_gift/gift_list/success');
		}
		
		public function code_gift($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			list($item) = $this->db_gift->gift_code_list();
			$this->cismarty->assign("item",$item);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,5));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open('manage_gift/gfit_code_detAll'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/gift/gift_code_list.html');
		}
		
		
		public function member_gift($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			list($item) = $this->db_gift->member_list();
			$this->cismarty->assign("item",$item);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
				
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,4));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open('manage_gift/member_detAll'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/gift/member_list.html');
		}
		
	}
?>